//
//  ST_TimeDownToors.m
//  ST_BaseMall
//
//  Created by 李宇廷 on 2022/9/11.
//

#import "ST_TimeDownToors.h"

@interface ST_TimeDownToors ()

/* timer */
@property (nonatomic, strong) NSTimer* timer;

/* timerBlock */
@property (nonatomic, copy) timerBlock block;

@end

@implementation ST_TimeDownToors

- (void)dealloc
{
    NSLog(@"%s", __FUNCTION__);
    NSLog(@"%@", self.class);
}

+ (ST_TimeDownToors *)shareTimer {
    static ST_TimeDownToors *timer = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        timer = [[ST_TimeDownToors alloc]init];
    });
    return timer;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)startTimerWithTime:(NSInteger)time complate:(timerBlock)complate {
    [self cancelTimer];
    if (time <= 0) {
        return;
    }
    _timeBlock = complate;
    _block = complate;
    _time = time;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Timered:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)Timered:(NSTimer *)timer {
    _time --;
//    NSLog(@"_time---_time ===> %ld",_time);
    if (self.block) {
        self.block(_time);
    }
    if (self.timeBlock) {
        self.timeBlock(_time);
    }
    if (_time == 0) {
        [self cancelTimer];
    }
}

- (void)cancelTimer {
    [_timer invalidate];
    _timer =nil;
}

- (void)setTime:(NSInteger)time {
    _time = time;
}


@end
